home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / mint104s.zoo / mint.src / main.c < prev    next >
C/C++ Source or Header  |  1993-03-08  |  39KB  |  1,460 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith.
  3. Copyright 1992,1993 Atari Corporation.
  4. All rights reserved.
  5. */
  6.  
  7. #include "mint.h"
  8. #include "version.h"
  9. #include "cookie.h"
  10. #include "xbra.h"
  11.  
  12. /* the kernel's stack size */
  13. #define STACK    8*1024L
  14.  
  15. /* if the user is holding down the magic shift key, we ask before booting */
  16. #define MAGIC_SHIFT 0x2        /* left shift */
  17.  
  18. /* magic number to show that we have captured the reset vector */
  19. #define RES_MAGIC 0x31415926L
  20.  
  21. static void xbra_install P_((xbra_vec *, long, long ARGS_ON_STACK (*)()));
  22. static void init_intr P_((void));
  23. static long getmch P_((void));
  24. static void do_line P_((char *));
  25. static void shutmedown P_((PROC *));
  26. void shutdown P_((void));
  27. static void doset P_((char *,char *));
  28. static long ARGS_ON_STACK mint_criticerr P_((long));
  29. static void ARGS_ON_STACK do_exec_os P_((register long basepage));
  30.  
  31. static int gem_active;    /* 0 if AES has not started, nonzero otherwise */
  32.  
  33. #define EXEC_OS 0x4feL
  34. static int  check_for_gem P_((void));
  35. static void run_auto_prgs P_((void));
  36.  
  37. #ifdef LATTICE
  38. /*
  39.  * AGK: this is witchcraft to completely replace the startup code for
  40.  * Lattice; doing so saves around 10K on the final binary and pulls only
  41.  * long division & multitplication from the library (and not even those
  42.  * if you compile for native '030). The drawback of this code is it
  43.  * passes no environment or command line whatsoever. Since I always
  44.  * set MiNT options & environment in 'mint.cnf' this is not a personal
  45.  * downer, however at some point in the future we ought to have a kernel
  46.  * parseargs() like call which sets these things up.
  47.  */ 
  48. BASEPAGE *_base;
  49.  
  50. static void
  51. start(BASEPAGE *bp)
  52. {
  53.     long shrinklen;
  54.     
  55.     _base = bp;
  56.     shrinklen = bp->p_tlen + bp->p_dlen + bp->p_blen + STACK + 0x100;
  57.     if (bp->p_lowtpa + shrinklen <= bp->p_hitpa) {
  58.         static char null[1] = {""};
  59.         static char *argv[2] = {null, NULL};
  60.         extern __builtin_putreg P_((int, long));    /* totally bogus */
  61.  
  62.         __builtin_putreg(15, bp->p_lowtpa + shrinklen);
  63.         Mshrink((void *)bp->p_lowtpa, shrinklen);
  64.         main(1, argv);
  65.     }
  66.     Pterm(ENSMEM);
  67. }
  68. #endif
  69.  
  70. #ifdef __GNUC__
  71. long _stksize = STACK;
  72. #ifndef PROFILING
  73. #include <minimal.h>
  74. #endif
  75. #endif
  76.  
  77. int curs_off = 0;    /* set if we should turn the cursor off when exiting */
  78. int mint_errno = 0;    /* error return from open and creat filesystem calls */
  79.  
  80. /*
  81.  * AGK: for proper co-processors we must consider saving their context.
  82.  * This variable when non-zero indicates that the BIOS considers a true
  83.  * coprocessor to be present. We use this variable in the context switch
  84.  * code to decide whether to attempt an FPU context save.
  85.  */
  86. short fpu = 0;
  87.  
  88. /*
  89.  * "mch" holds what kind of machine we are running on
  90.  */
  91. long mch = 0;
  92.  
  93. /*
  94.  * "screen_boundary+1" tells us how screens must be positioned
  95.  * (to a 256 byte boundary on STs, a 16 byte boundary on other
  96.  * machines; actually, 16 bytes is conservative, 4 is probably
  97.  * OK, but it doesn't hurt to be cautious). The +1 is because
  98.  * we're using this as a mask in the ROUND() macro in mem.h.
  99.  */
  100. int screen_boundary = 255;
  101.  
  102. /*
  103.  * variable holds processor type
  104.  */
  105. long mcpu = 0;
  106.  
  107. /*
  108.  * variable holds language preference
  109.  */
  110. int gl_lang = -1;
  111.  
  112. /*
  113.  * variable set if someone has already installed an flk cookie
  114.  */
  115. int flk = 0;
  116.  
  117. /*
  118.  * variable set to 1 if the _VDO cookie indicates Falcon style video
  119.  */
  120. int FalconVideo;
  121.  
  122. /* program to run at startup */
  123. #ifdef MULTITOS
  124. static int init_is_gemsys = 1;    /* set to 1 if init is gem.sys */
  125. #else
  126. static int init_is_gemsys = 0;    /* set to 1 if init is gem.sys */
  127. #endif
  128. static const char *init_prg = 0;
  129.  
  130. /* note: init_tail is also used as a temporary stack for resets in
  131.  * intr.spp
  132.  */
  133. char init_tail[256];
  134.  
  135. /* initial environment for that program */
  136. static char *init_env = 0;
  137. /* temporary pointer into that environment for setenv */
  138. static char *env_ptr;
  139. /* length of the environment */
  140. static long env_len;
  141.  
  142. /* GEMDOS pointer to current basepage */
  143. BASEPAGE **tosbp;
  144.  
  145. /* pointer to the BIOS keyboard shift variable */
  146. extern char *kbshft;    /* see bios.c */
  147.  
  148. /* version of TOS we're running over */
  149. int tosvers;
  150.  
  151. /* structures for keyboard/MIDI interrupt vectors */
  152. KBDVEC *syskey, oldkey;
  153. xbra_vec old_ikbd;            /* old ikbd vector */
  154.  
  155. /* values the user sees for the DOS, BIOS, and XBIOS vectors */
  156. long save_dos, save_bios, save_xbios;
  157.  
  158. /* values for original system vectors */
  159. xbra_vec old_dos, old_bios, old_xbios, old_timer, old_vbl, old_5ms;
  160. xbra_vec old_criticerr;
  161. xbra_vec old_execos;
  162.  
  163. long old_term;
  164.  
  165. xbra_vec old_resvec;    /* old reset vector */
  166. long old_resval;    /* old reset validation */
  167.  
  168. #ifdef EXCEPTION_SIGS
  169. /* bus error, address error, illegal instruction, etc. vectors */
  170. xbra_vec old_bus, old_addr, old_ill, old_divzero, old_trace, old_priv;
  171. xbra_vec old_linef, old_chk, old_trapv, old_mmuconf, old_format, old_cpv;
  172. xbra_vec old_uninit, old_spurious, old_fpcp[7], old_pmmuill, old_pmmuacc;
  173. #endif
  174.  
  175. /* BIOS disk vectors */
  176. xbra_vec old_mediach, old_getbpb, old_rwabs;
  177.  
  178. /* BIOS drive map */
  179. long olddrvs;
  180.  
  181. extern Func bios_tab[], dos_tab[];
  182.  
  183. /* kernel info that is passed to loaded file systems and device drivers */
  184.  
  185. struct kerinfo kernelinfo = {
  186.     MAJ_VERSION, MIN_VERSION,
  187.     DEFAULT_MODE, 0,
  188.     bios_tab, dos_tab,
  189.     changedrv,
  190.     Trace, Debug, ALERT, FATAL,
  191.     kmalloc, kfree, umalloc, ufree,
  192.     strnicmp, stricmp, strlwr, strupr, ksprintf,
  193.     ms_time, unixtim, dostim,
  194.     nap, sleep, wake, wakeselect,
  195.     denyshare, denylock
  196. };
  197.  
  198. /* table of processor frame sizes in _words_ (not used on MC68000) */
  199. unsigned char framesizes[16] = {
  200. /*0*/    0,    /* MC68010/M68020/M68030/M68040 short */
  201. /*1*/    0,    /* M68020/M68030/M68040 throwaway */
  202. /*2*/    2,    /* M68020/M68030/M68040 instruction error */
  203. /*3*/    2,    /* M68040 floating point post instruction */
  204. /*4*/    3,    /* MC68LC040/MC68EC040 unimplemented floating point instruction */
  205. /*5*/    0,    /* NOTUSED */
  206. /*6*/    0,    /* NOTUSED */
  207. /*7*/    26,    /* M68040 access error */    
  208. /*8*/    25,    /* MC68010 long */    
  209. /*9*/    6,    /* M68020/M68030 mid instruction */
  210. /*A*/    12,    /* M68020/M68030 short bus cycle */
  211. /*B*/    42,    /* M68020/M68030 long bus cycle */
  212. /*C*/    8,    /* CPU32 bus error */
  213. /*D*/    0,    /* NOTUSED */
  214. /*E*/    0,    /* NOTUSED */
  215. /*F*/    0    /* NOTUSED */
  216. };
  217.  
  218. /* TOS and MiNT cookie jars, respectively. See the comments and code 
  219.  * after main() for further details
  220.  */
  221.  
  222. COOKIE *oldcookie, *newcookie;
  223.  
  224. /*
  225.  * install a new vector for address "addr", using the XBRA protocol.
  226.  * must run in supervisor mode!
  227.  */
  228.  
  229. static void
  230. xbra_install(xv, addr, func)
  231.     xbra_vec *xv;
  232.     long addr;
  233.     long ARGS_ON_STACK (*func)();
  234. {
  235.     xv->xbra_magic = XBRA_MAGIC;
  236.     xv->xbra_id = MINT_MAGIC;
  237.     xv->jump = JMP_OPCODE;
  238.     xv->this = func;
  239.     xv->next = *((struct xbra **)addr);
  240.     *((short **)addr) = &xv->jump;
  241. }
  242.  
  243. /*
  244.  * MiNT critical error handler; all it does is to jump through
  245.  * the vector for the current process
  246.  */
  247.  
  248. static long ARGS_ON_STACK
  249. mint_criticerr(error)
  250.     long error;    /* high word is error, low is drive */
  251. {
  252.     return (*curproc->criticerr)(error);
  253. }
  254.  
  255. /*
  256.  * if we are MultiTOS, and if we are running from the AUTO folder,
  257.  * then we grab the exec_os vector and use that to start GEM; that
  258.  * way programs that expect exec_os to act a certain way will still
  259.  * work.
  260.  * NOTE: we must use Pexec instead of p_exec here, because we will
  261.  * be running in a user context (that of process 1, not process 0)
  262.  */
  263.  
  264. static void ARGS_ON_STACK
  265. do_exec_os(basepage)
  266.     register long basepage;
  267. {
  268.     register int r;
  269.  
  270.     if (!init_prg)
  271.         init_prg = "\\multitos\\gem.sys";
  272.  
  273. /* we have to set a7 to point to lower in our TPA; otherwise we would
  274.  * bus error right after the Mshrink call!
  275.  */
  276.     setstack(basepage+500L);
  277. #ifdef __TURBOC__
  278.     Mshrink(0, (void *)basepage, 512L);
  279. #else
  280.     Mshrink((void *)basepage, 512L);
  281. #endif
  282.     r = Pexec(200, init_prg, init_tail, init_env);
  283.     Pterm(r);
  284. }
  285.  
  286.  
  287. /* initialize all interrupt vectors and new trap routines
  288.  * we also get here any TOS variables that we're going to change
  289.  * (e.g. the pointer to the cookie jar) so that rest_intr can
  290.  * restore them.
  291.